package com.fjrz.bbxwj.popup

import android.content.Context
import android.text.Editable
import android.view.View
import android.view.animation.Animation
import android.view.animation.TranslateAnimation
import androidx.core.widget.addTextChangedListener
import com.blankj.utilcode.util.BarUtils
import com.fjrz.bbxwj.R
import kotlinx.android.synthetic.main.popup_edit_user.view.*
import razerdp.basepopup.BasePopupWindow

class EditUserPopup(
    context: Context,
    val onConfirm: (content: String, editType: Int) -> Unit
) : BasePopupWindow(context) {

    init {
        setOverlayNavigationBar(false)
        setAdjustInputMethod(false)
    }

    private var mEditType: Int = 0


    override fun onCreateContentView(): View {
        return createPopupById(R.layout.popup_edit_user)
    }

    override fun onViewCreated(contentView: View) {
        super.onViewCreated(contentView)
        BarUtils.addMarginTopEqualStatusBarHeight(contentView.flEditUserBar)

        contentView.ivEditUserBack.setOnClickListener {
            dismiss()
        }
        contentView.etEditUserContent.addTextChangedListener {
            contentView.tvEditUserConfirm.isEnabled = !it.isNullOrEmpty()
        }
        contentView.tvEditUserConfirm.setOnClickListener {
            onConfirm(contentView.etEditUserContent.text.toString(), mEditType)
        }
    }

    override fun onCreateShowAnimation(width: Int, height: Int): Animation {
        return TranslateAnimation(
            Animation.RELATIVE_TO_PARENT, 0f,
            Animation.RELATIVE_TO_PARENT, 0f,
            Animation.RELATIVE_TO_PARENT, 1f,
            Animation.RELATIVE_TO_PARENT, 0f
        ).apply { duration = 300L }
    }

    override fun onCreateDismissAnimation(): Animation {
        return TranslateAnimation(
            Animation.RELATIVE_TO_PARENT, 0f,
            Animation.RELATIVE_TO_PARENT, 0f,
            Animation.RELATIVE_TO_PARENT, 0f,
            Animation.RELATIVE_TO_PARENT, 1f
        ).apply { duration = 300L }
    }

    fun setTitle(title: String): EditUserPopup {
        contentView.tvEditUserTitle.text = title
        return this
    }

    fun setHint(hint: String?): EditUserPopup {
        contentView.etEditUserContent.hint = hint
        return this
    }

    fun setText(text: String?): EditUserPopup {
        contentView.etEditUserContent.text = Editable.Factory.getInstance().newEditable(text)
        return this
    }

    fun setType(editType: Int): EditUserPopup {
        this.mEditType = editType
        return this
    }

    override fun showPopupWindow() {
        super.showPopupWindow()
    }

}